<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    handleOfficeLineCharts:{
      type: Function,
      default: null
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
    
      var that = this;
      this.chart.on('click',function(params){
        var answer = params.seriesName;
        var number = params.name;
        var countValue = params.value;
        var index = params.dataIndex;
        var data = that.chartData.zdata[index];
        var params = that.$parent.addDateRange(that.$parent.queryParams,that.$parent.dateRange);
 　　　  that.handleOfficeLineCharts(params);//调用接口
      })
      
    },
    setOptions( {xdata,ydata,zdata,adata,bdata,pdata,tdata,jdata}  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },
            // formatter: function(params){
            //   var index = params[0].dataIndex;
            //   return params[0].axisValue+"："+params[0].value+"</br>";
            // }
        },
        title: {
            text: bdata,
            textStyle: {
              fontSize: 15,
              color: "#d5e2f6"
            },
            left: '20',
            top: "12"
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: "category",
            axisLine: {
                lineStyle: {
                    color: '#4693EC'
                }
            },
            splitLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            splitArea: {
                show: false
            },
            axisLabel: {
                interval: 0,
                rotate:"90",
                color: "#798596",
                fontSize:12
            },
            data: xdata,
        }],
        yAxis: [{
            type: "value",
            splitLine: {
               show: false
            },
            axisLine: {
                lineStyle: {
                    color: '#4693EC'
                }
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                interval: 0,
                rotate:"45",
                color: "#00FFE3",
                fontSize:12
            },
            splitArea: {
                show: false
            },

        }],
        series: [
          {
            name: "日常工作",
            type: 'line',
            smooth: true, //是否平滑
            showAllSymbol: false,
            symbol: 'circle',
            symbolSize: 2,
            lineStyle: {
                normal: {
                    color: "#f3b058",
                },
            },
            itemStyle: {
                color: "#f3b058",
            },
            tooltip: {
                show: true
            },
            areaStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#FFA500'
                        },
                        {
                            offset: 1,
                            color: '#FFA500'
                        }
                    ], false),
                    shadowColor: '#FFA500',
                    shadowBlur: 20
                }
            },
            data: ydata,
          markLine: {
              silent: true,
              lineStyle: {
                normal: {
                  color: '#01fef9'                   // 这儿设置安全基线颜色
                }
              },
              data: [{
                  yAxis: jdata[0]
              }],
              label: {
                normal: {
                  formatter: jdata[0]+'h (基准工时)',             // 这儿设置安全基线
                  position: 'insideEndTop'           
                }
              },
          },

        },
        // {
        //     name: "项目工作",
        //     type: 'line',
        //     smooth: true, //是否平滑
        //     showAllSymbol: false,
        //     symbol: 'circle',
        //     symbolSize: 2,
        //     lineStyle: {
        //         normal: {
        //             color: "#a0ffff",
        //         },
        //     },
        //     itemStyle: {
        //         color: "#00b3f4",
        //     },
        //     tooltip: {
        //         show: true
        //     },
        //     areaStyle: {
        //         normal: {
        //             color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        //                     offset: 0,
        //                     color: 'rgba(0,202,149,0.3)'
        //                 },
        //                 {
        //                     offset: 1,
        //                     color: 'rgba(0,202,149,0)'
        //                 }
        //             ], false),
        //             shadowColor: 'rgba(0,202,149, 0.9)',
        //             shadowBlur: 20
        //         }
        //     },
        //     data: pdata
        // },
        {
            name: "总量",
            type: 'line',
            smooth: true, //是否平滑
            showAllSymbol: false,
            symbol: 'circle',
            symbolSize: 2,
            lineStyle: {
                normal: {
                    color: "#FF99CC",
                },
            },
            itemStyle: {
                color: "#FF99CC",
            },
            tooltip: {
                show: true
            },
            areaStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: 'rgba(0,202,149,0.3)'
                        },
                        {
                            offset: 1,
                            color: 'rgba(0,202,149,0)'
                        }
                    ], false),
                    shadowColor: 'rgba(0,202,149, 0.9)',
                    shadowBlur: 20
                }
            },
            data: tdata
        }
        
    ]
     })
    }
  }
}
</script>
